//
//  TopViewController.m
//  SAT_NET
//
//  Created by lanou3g on 16/3/7.
//  Copyright © 2016年 郭丹. All rights reserved.
//

#import "TopViewController.h"
#import "RequestProRank.h"
#import "DetailCell.h"
#import <UIImageView+WebCache.h>
#import "DetailModel.h"
#import "DetailAwardModel.h"
#import <MJRefresh.h>

static NSString *detail_id = @"detail_id";

@interface TopViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>

@property(nonatomic, strong)NSMutableArray *arrayAllData;
@property(nonatomic, assign)NSInteger page;
@property(nonatomic, assign)NSInteger bottomNum;

@end

@implementation TopViewController

- (void)loadView {
    self.topV = [[TopView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.topV;
    self.page = 0;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setViews];
    [self setdelegate];
    
    [self headerAction];
}

- (void)setViews {
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self];
        nav.view.frame = CGRectMake(0, 0, kScreenW, 64);
    nav.navigationBar.frame = CGRectMake(0, 0, kScreenW, 64);
    nav.navigationBar.barTintColor = [UIColor colorWithRed: 45 / 255.0
                                                     green: 128 / 255.0
                                                      blue: 248 / 255.0
                                                     alpha:1];
    nav.navigationBar.translucent = YES;
    // 交互被nav.view截获，调整位置
//    [self.view insertSubview:nav.view atIndex:0];
    [self.view addSubview:nav.view];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrow"] style:(UIBarButtonItemStyleDone) target:self action:@selector(leftAction)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    [self.topV.tableView registerNib:[UINib nibWithNibName:@"DetailCell" bundle:nil] forCellReuseIdentifier:@"detail_id"];
    
    self.topV.tableView.rowHeight = UITableViewAutomaticDimension;
    self.topV.tableView.estimatedRowHeight = 2;
    
    
    self.topV.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerAction)];
    self.topV.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerAction)];
    
}

- (void)headerAction {
    
    NSInteger section = [self.dicUrl[@"section"] integerValue];
    NSString *subcateId = self.dicUrl[@"subcateId"];
    NSString *url = nil;
    if (section == 0) {
        url = [NSString stringWithFormat:@"%@%d&subcateId=%@",kProRank,0,subcateId];
    } else if (section == 1) {
        url = [NSString stringWithFormat:@"%@%d&priceId=noPrice&subcateId=%@",kAllList,0,subcateId];
    } else {
        url = [NSString stringWithFormat:@"%@%@%@%d%@%@",kAdvancedList1,self.dicUrl[@"ID"],kAdvancedList2,0,kAdvancedList3,subcateId];
    }
    [self setDataWithUrl:url];
    
}


- (void)footerAction {
    
    NSInteger section = [self.dicUrl[@"section"] integerValue];
    NSString *subcateId = self.dicUrl[@"subcateId"];
    NSString *url = nil;
    self.page ++;
    if (section == 0) {
        url = [NSString stringWithFormat:@"%@%ld&subcateId=%@",kProRank,self.page,subcateId];
    } else if (section == 1) {
        url = [NSString stringWithFormat:@"%@%ld&priceId=noPrice&subcateId=%@",kAllList,self.page,subcateId];
    } else {
        url = [NSString stringWithFormat:@"%@%@%@%ld%@%@",kAdvancedList1,self.dicUrl[@"ID"],kAdvancedList2,self.page,kAdvancedList3,subcateId];
    }
    [self setDataWithUrl:url];
    
}



- (void)leftAction {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)setdelegate {
    self.topV.tableView.delegate = self;
    self.topV.tableView.dataSource = self;
}

- (void)setDataWithUrl:(NSString *)url {
    
    RequestProRank *requestPR = [[RequestProRank alloc] init];
    [requestPR requestWithUrl:url success:^(id item1, id item2) {
        
        CGFloat num = [item2 integerValue] / 20.0;
        //向上取整
        self.bottomNum = ceilf(num);
        if ([item1 count] == 0) {
            [self.topV.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            for (id item in item1) {
                [self.arrayAllData addObject:item];
            }
            [self.topV.tableView reloadData];
            [self.topV.tableView.mj_header endRefreshing];
            [self.topV.tableView.mj_footer endRefreshing];
        }
        
    } fail:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
}

#pragma mark tableview delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrayAllData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DetailModel *detailM = self.arrayAllData[indexPath.row];
    
    DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:detail_id forIndexPath:indexPath];
    [cell.imageV sd_setImageWithURL:detailM.pic placeholderImage:[UIImage imageNamed:@"cancel.png"]];
    cell.name.text = detailM.name;
    if ([detailM.price isEqualToString:@"概念产品"] == YES) {
        cell.price.text = [NSString stringWithFormat:@"%@",detailM.price];
    } else {
        cell.price.text = [NSString stringWithFormat:@"%@%@起",detailM.mark,detailM.price];
    }
    
    cell.reviewNum.text = [NSString stringWithFormat:@"%@点评",detailM.reviewNum];
    CGFloat hit = [detailM.thisWeekHit floatValue];
    if (hit < 10000) {
        cell.thisWeekHit.text = [NSString stringWithFormat:@"周关注量%.0f次",hit];
    } else {
        cell.thisWeekHit.text = [NSString stringWithFormat:@"周关注量%.1f万次",hit / 10000];
    }
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //WebView调整
    
    
}

#pragma mark scroller代理
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    self.topV.bottomLable.hidden = NO;
    self.topV.bottomLable.text = [NSString stringWithFormat:@"%ld/%ld",self.page+1,self.bottomNum];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (!decelerate) {
        self.topV.bottomLable.hidden = YES;
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.topV.bottomLable.hidden = YES;
}

- (NSMutableArray *)arrayAllData {
    if (!_arrayAllData) {
        _arrayAllData = [NSMutableArray array];
    }
    return _arrayAllData;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
